問題描述
如何將帖子從 Wordpress 導入 Wagtail 2(Draftail 編輯器),包括圖像? (How to import posts from Wordpress to Wagtail 2 (Draftail editor) including images?)
我正在嘗試將帖子從 Wordpress 導入 Wagtail,包括圖像。
我意識到 Draftail 編輯器使用專用標籤保存圖像這一事實,例如:
<embed alt="fonctionnement‑pim‑schema‑640.png" embedtype="image" format="fullwidth" id="412" />
當我從 Wordpress 導入我的帖子時,我將 img 標籤轉換為嵌入標籤。下面是我的代碼片段:
resp = requests.get(img["src"], stream=True)
if resp.status_code != requests.codes.ok:
print("Unable to import " + img["src"])
continue
fp = BytesIO()
fp.write(resp.content)
image = Image(title=file_name, width=width, height=height)
image.file.save(file_name, File(fp))
image.save()
try:
embed_id = image.get_rendition("original").id
embed_alt = image.get_rendition("original").alt
new_tag = soup.new_tag('embed', alt=f'{embed_alt}', embedtype="image", format="fullwidth", id=f'{embed_id}')
img.replace_with(new_tag)
它似乎有效。當我檢查數據庫時,所有 img
標記都被替換為格式正確的嵌入標記,並且所有圖像都被下載到媒體文件夾中。
不幸的是,當我檢查管理區域時。嵌入標籤存在但無法識別圖片: